package com.example.jdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EmpDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;



    public void add() {
        String sql = "INSERT INTO book(id, name, price, author, pubDate) VALUES (?,?,?,?,?)";
        jdbcTemplate.update(sql, 0, "kk", 32, "lzj", "2019-01-08 13:15:18");
    }


    public void get() {
        String sql = "SELECT id, `name`, price, author, pubDate  FROM book WHERE id = ?";
        RowMapper<Book> rowMapper = new BeanPropertyRowMapper<>(Book.class);
        Book book = jdbcTemplate.queryForObject(sql, rowMapper, 10);
        System.out.println(book);
    }

    public void getList() {
        String sql = "SELECT id, `name`, price, author, pubDate  FROM book ";
        RowMapper<Book> rowMapper = new BeanPropertyRowMapper<>(Book.class);
        List<Book> bookList = jdbcTemplate.query(sql, rowMapper);
        System.out.println(bookList);
    }

    public void getCount() {
        Integer count = jdbcTemplate.queryForObject("select count(*) from book", Integer.class);
        System.out.println(count);
    }
}



